package com.example.myweb.Controller.Auth;

import com.example.myweb.Common.ApiResponse;
import com.example.myweb.Domain.Entity.User;
import com.example.myweb.Dto.LikeRequestDTO;
import com.example.myweb.Service.Auth.LikeService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@RestController
@RequestMapping("/likes")
public class LikeController {

    @Autowired
    private LikeService likeService;

    /**
     * 点赞一个目标（文章、动态等）
     */
    @PostMapping
    public ResponseEntity<ApiResponse<Void>> like(
            @Valid @RequestBody LikeRequestDTO request,
            @AuthenticationPrincipal User currentUser) {

        likeService.likeTarget(request.getTargetType(), request.getTargetId(), currentUser);
        return ResponseEntity.ok(ApiResponse.success("点赞成功"));
    }

    /**
     * 取消点赞一个目标
     */
    @DeleteMapping
    public ResponseEntity<ApiResponse<Void>> unlike(
            @Valid @RequestBody LikeRequestDTO request,
            @AuthenticationPrincipal User currentUser) {

        likeService.unlikeTarget(request.getTargetType(), request.getTargetId(), currentUser);
        return ResponseEntity.ok(ApiResponse.success("取消点赞成功"));
    }
}
//@RestController
//@RequestMapping("/likes") // 资源路径为 /likes
//public class LikeController {
//    @Autowired
//    private LikeService likeService;
//
//    // DTO 用于接收请求，增强代码可读性和校验能力
//    @Data
//    public static class LikeRequest {
//        @NotBlank
//        private String targetType;
//        @NotNull
//        private Long targetId;
//    }
//
//    @PostMapping
//    public ResponseEntity<ApiResponse<Void>> like(
//            @Valid @RequestBody LikeRequest request,
//            @AuthenticationPrincipal User currentUser) {
//        likeService.likeTarget(request.getTargetType(), request.getTargetId(), currentUser);
//        return ResponseEntity.ok(ApiResponse.success(null, "点赞成功"));
//    }
//
//    // 注意：HTTP规范中DELETE请求通常不带body，但很多框架支持。
//    // 如果客户端不支持，也可以改为 POST /likes/unlike
//    @DeleteMapping
//    public ResponseEntity<ApiResponse<Void>> unlike(
//            @Valid @RequestBody LikeRequest request,
//            @AuthenticationPrincipal User currentUser) {
//        likeService.unlikeTarget(request.getTargetType(), request.getTargetId(), currentUser);
//        return ResponseEntity.ok(ApiResponse.success(null, "取消点赞成功"));
//    }
//}